package cn.middle.basic.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.middle.common.base.mapper.BaseMapper;
import com.middle.common.base.service.impl.BaseServiceImpl;
import com.middle.common.result.Page;
import com.middle.common.result.UserInfo;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.middle.basic.entity.SysMenu;
import cn.middle.basic.entity.SysRole;
import cn.middle.basic.entity.SysUser;
import cn.middle.basic.mapper.SysMenuMapper;
import cn.middle.basic.mapper.SysRoleMapper;
import cn.middle.basic.mapper.SysUserMapper;
import cn.middle.basic.service.SysUserService;

@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUser> implements SysUserService {

	@Autowired
	private SysUserMapper mapper;
	
	@Autowired
	private SysMenuMapper menuMapper;
	
	@Autowired
	private SysRoleMapper roleMapper;
	
	@Override
	public BaseMapper<SysUser> getDao() {
		return mapper;
	}

	@Override
	public SysUser login(String username, String password) {
	    SysUser sysUser = mapper.loginByUserName(username);
	    if(sysUser != null) {
	    	String decryptPassword = SaSecureUtil.aesEncrypt(sysUser.getExpand1(), password);
	    	if(sysUser.getPassword().equals(decryptPassword)) {
	    		return sysUser;
	    	}
	    }
		return null;
	}

	@Override
	public boolean checkLoginName(String username) {
		return mapper.loginByUserName(username) == null;
	}


	@Override
	public UserInfo<SysMenu,SysRole> getUserInfo(Long userId) {
		UserInfo<SysMenu,SysRole> userInfo = new UserInfo<>();
		SysUser user = mapper.selectByPrimaryKey(userId);
		if(user != null) {
			userInfo.setUserId(userId);
			userInfo.setAvatar(user.getAvatar());
			userInfo.setRealName(user.getRealName());
			List<SysMenu> menus =  menuMapper.selectMenuByUserId(userId);
			userInfo.setMenus(menus);
			List<String> permissions = menuMapper.selectAuthorisationCodeByUserId(userId);
			userInfo.setPermissions(permissions);
			List<SysRole> roles = roleMapper.rolesByUser(userId);
			userInfo.setRoles(roles);
			return userInfo;
		}
		return null;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void delete(Long userId) {
		this.mapper.disarmRole(userId);
		this.mapper.deleteByPrimaryKey(userId);
	}
	
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void associationUserRoles(Long userId, List<Long> roleIds) {
		for(Long roleId : roleIds) mapper.associationUserRole(userId, roleId);
	}

	@Override
	public List<SysUser> usersByRoleId(Long roleId) {
		return mapper.usersByRoleId(roleId);
	}

	@Override
	public Page<SysUser> listConditions(int page, int pageSize, String realName, String accountName, String phone, Byte status) {
		PageHelper.startPage(page,pageSize);
		List<SysUser> users = mapper.listConditions(realName, accountName, phone, status);
		return new Page<SysUser>(new PageInfo<SysUser>(users));
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void saveUserAndRole(SysUser user, List<Long> roleIds) {
		mapper.save(user);
		for(Long roleId : roleIds) mapper.associationUserRole(user.getId(), roleId);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void updateUserAndRole(SysUser user, List<Long> roleIds) {
		mapper.updateByPrimaryKey(user);
		mapper.disarmRole(user.getId());
		for(Long roleId : roleIds) mapper.associationUserRole(user.getId(), roleId);
	}
	

}
